package Fifteen;
////////////////////////////////////////////////////////////////////////////////
//  David Blair
//  3/26/2010
//  CIS 296
//  Development in Java
//  Assignment 3 
//  Program Name: Fifteen Applet and Application
//
//  This program is a slide puzzle applet and application that presents the user
//  with fifteen slides, labeled "1" through "15" in a square array with one 
//  slide piece missing. The user may begin sliding pieces around immediately as
//  long as there is empty space for the slider to occupy. Sliding buttons may 
//  be dragged and when released the slider slides to the empty space it most 
//  occupies. There are scramble and reset buttons at the bottom. The scramble
//  button may always be clicked and moves sliders from 1 to 15 times from any 
//  given initial position. The reset button may always be clicked and resets
//  the puzzle to its original position. If, by sliding a slider button, the  
//  user produces the original layout of the sliders a frame pops up saying that
//  the puzzle is solved. The user may then close the solved frame and play 
//  again.
//      Applet: To use as an applet place class files Fifteen.class, 
//  MovableButton.class, and PuzzleLogic.class, the HTML file Fifteen.html, and
//  the jpg image Penguins.jpg in a folder classes that is in the working  
//  directory of your jdk. Upon double clicking Fifteen.html a webpage will open
//  (provided the browser uses java) and the user will see a message,"Have some 
//  slide puzzle fun!" followed by the slide puzzle, followed by an image of 
//  penguins, followed by a message,"And some penguins just cause!"
//      Application: To use as an application, place Fifteen.java in your 
//  working jdk directory, type "javac Fifteen.java" and then type
//  "java Fifteen".
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//  IMPORTED PACKAGES
////////////////////////////////////////////////////////////////////////////////
import java.awt.event.ActionListener;   //  for timer events
import javax.swing.*;                   //  for buttons and other gui facilities
import java.awt.event.*;                //  for action events
import java.awt.*;                      //  for layouts
////////////////////////////////////////////////////////////////////////////////
//  END IMPORTED PACKAGES
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
//  MAIN CLASS FIFTEEN
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////    
//  Begin definition class Fifteen
//      Class Fifteen extends JApplet so may work as a java applet and also
//  includes a method main so that it may operate as an application. Fifteen
//  sets the UI and puzzle logic up.
////////////////////////////////////////////////////////////////////////////////
public class Fifteen extends JApplet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public PuzzleLogic puzzle = new PuzzleLogic();  //  for puzzle logic

	////////////////////////////////////////////////////////////////////////////
	//  Begin definition method init
	//      init is called by a web browser to initialize the UI.
	////////////////////////////////////////////////////////////////////////////
	public void init(){
		//  create puzzle UI
		JPanel puzzlePanel = new JPanel();                  //  add JPanel for
															//  buttons to be 
															//  added

		GridLayout layout1 = new GridLayout(4, 4, 0, 0);    //  make a  
															//  GridLayout
															//  object

		puzzlePanel.setLayout(layout1);                     //  set puzzlePanel
															//  layout to
															//  layout1
		//  for each visible button we want to have
		for( int i = 0; i < 15; i++){
			//  make a MovableButton button and initialize with i + 1
			//  and PuzzleLogic object puzzle
			MovableButton button = new MovableButton(i + 1, puzzle);
			//  add button to puzzlePanel
			puzzlePanel.add(button);
			//  initialize button original positions
			button.origPos[0] = (i - 4 * (i/4));
			button.origPos[1] = i/4;
			//  initialize puzzleArray positions to refer to proper buttons
			puzzle.puzzleArray[(i - 4 * (i/4))][i/4] = button;
			//  initialize buttonTrack to buttons
			puzzle.buttonTrack[i] = button;
		}   //  end for( int i = 0; i < 15; i++)
		//  set empty space invisible button properties
		puzzle.buttonTrack[15] = puzzle.puzzleArray[3][3] 
				= new MovableButton(0, puzzle);

		//  create optionPanel JPanel for option buttons
		JPanel optionPanel = new JPanel();
		//  create FlowLayout object layout2 
		FlowLayout layout2 = new FlowLayout();
		//  set optionPanel layout to layout2
		optionPanel.setLayout(layout2);
		//  create new JButton scramble with label "Scramble"
		JButton scramble = new JButton("Scramble");
		//  set text of scramble to Scramble
		scramble.setText("Scramble");

		//  add new ActionListener to scramble and set behavior
		scramble.addActionListener(new ActionListener(){
			//  override method actionPerformed
			public void actionPerformed(ActionEvent e){
				//  when clicked invoke puzzle method Scramble()
				System.out.println("scramble button pressed");
				puzzle.Scramble();
			}   //  end public void actionPerformed
		}); //  end scramble.addActionListener(new ActionListener()
		//  add scramble JButton to optionPanel
		optionPanel.add(scramble);

		//  create new JButton reset
		JButton reset = new JButton("Reset");
		//  set text of rest to "Reset"
		reset.setText("Reset");
		//  add ActionListener to reset
		reset.addActionListener(new ActionListener(){
			//  override method action performed
			public void actionPerformed(ActionEvent e){
				//  when clicked invoke method Reset()
				Reset();
			}   //  end public void actionPerformed(ActionEvent e)
		}); //  end reset.addActionListener(new ActionListener()
		optionPanel.add(reset);

		//  set layout of JApplet and add components

		//  set layout to new BorderLayout
		setLayout(new BorderLayout(5,10));
		//  add puzzlePanel to the Fifteen Applet
		add(puzzlePanel, BorderLayout.CENTER);
		//  add optionPanel to the Fifteen Applet
		add(optionPanel, BorderLayout.SOUTH);
	}   //  end public void init()

	////////////////////////////////////////////////////////////////////////////
	//  Begin definition method main()
	//      Method main makes Fifteen runnable as an application
	////////////////////////////////////////////////////////////////////////////
	public static void main(String[] args) {
		//  create a new JFrame frame and label it Fifteen
		JFrame frame = new JFrame("Fifteen");
		//  create a Fifteen object
		Fifteen fifteen = new Fifteen();
		//  add fifteen to center of frame with BordeLayout
		frame.add(fifteen, BorderLayout.CENTER);
		//  invoke init()
		fifteen.init();
		//  invoke start()
		fifteen.start();

		//  set frame size
		frame.setSize(400,400);
		//  set location relative to null
		frame.setLocationRelativeTo(null);
		//  set exit on close
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//  set the frame as visible
		frame.setVisible(true);
	}   //  end definition public static void main(String[] args)

	////////////////////////////////////////////////////////////////////////////
	//  Begin definition of method Reset()
	//      Method Reset() resets the GUI, the puzzle logic, and the buttons
	////////////////////////////////////////////////////////////////////////////
	public void Reset(){
		//  reset the frame
		this.validate();
		//  reset the puzzle logic
		puzzle.Reset();
		//  reset each button
		for( int i = 0; i < 16; i++){
			puzzle.buttonTrack[i].Reset();
		}   //  end for( int i = 0; i < 16; i++)
	}   //  end definition method public void Reset()
}   //  end definition public class Fifteen extends JApplet

////////////////////////////////////////////////////////////////////////////////
//  END CLASS FIFTEEN
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//  END PROGRAM
////////////////////////////////////////////////////////////////////////////////